<?php
namespace app\console\controller;
use app\console\controller\Auth;
use think\facade\Config;
use think\Db;
use app\service\controller\File;

/**
 * 首页设置
 * */
class Home extends Auth
{
    // pc端
    public function banner_list(){
        $lists=Db::table('tp_banner')
            ->field('tp_navigation.title,tp_banner.*')
            ->leftJoin('tp_navigation','tp_navigation.id=tp_banner.navigation_id')
            ->order('navigation_id')
            ->paginate(10);

        $this->assign('active', 'home');
        $this->assign([
            'topbar' => ['首页设置', 'banner设置'],
            'seting' => Config::pull('cms_config'),
            'lists'  =>$lists
        ]);
        return $this->fetch();
    }
    // pc banner添加
    public function banner_add(){
        if ($this->request->isAjax()) {
            $data = $this->request->post();
            //上传图片
            $cate_img = (new \app\service\controller\File())->upload_single_img('head_img', 'head_img');
            if (is_array($cate_img)) {
                if ($cate_img['code'] == 1) {
                    $data['head_img'] = $cate_img['img_path'];
                } else {
                    $this->error($cate_img['msg']);
                }
            } else {
                unset($data['head_img']);
            }
            //更新栏目
            $res=Db::table('tp_banner')->insert($data);
            $this->success('新增成功');
            exit;
        }
        $list=Db::table('tp_navigation')->select();
        $this->assign([
            'topbar' => ['首页设置', 'banner设置','添加'],
            'list'=>$list
        ]);
        return $this->fetch();
    }
    //pc banner修改
    public function banner_edit($id){
        if ($this->request->isAjax()) {
            $data = $this->request->post();
            //上传图片
            $cate_img = (new \app\service\controller\File())->upload_single_img('head_img', 'head_img');
            if (is_array($cate_img)) {
                if ($cate_img['code'] == 1) {
                    $data['head_img'] = $cate_img['img_path'];
                } else {
                    $this->error($cate_img['msg']);
                }
            } else {
                unset($data['head_img']);
            }
            //更新栏目
            $res=Db::table('tp_banner')->where('id',$id)->update($data);
            $this->success('修改成功');
            exit;
        }
        $list=Db::table('tp_navigation')->select();
        $res=Db::table('tp_banner')->where('id',$id)->find();
        $this->assign([
            'topbar' => ['首页设置','banner设置','修改'],
            'seting' => Config::pull('cms_config'),
            'list'   =>$res,
            'lists'    =>$list
        ]);
        return $this->fetch();
    }

    // 移动端
    public function banner_copy_list(){
        $lists=Db::table('tp_banner_copy')
            ->field('tp_navigation.title,tp_banner_copy.*')
            ->leftJoin('tp_navigation','tp_navigation.id=tp_banner_copy.navigation_id')
            ->order('navigation_id')
            ->paginate(10);

        $this->assign('active', 'home');
        $this->assign([
            'topbar' => ['首页设置', '移动banner设置'],
            'seting' => Config::pull('cms_config'),
            'lists'  =>$lists
        ]);
        return $this->fetch();
    }
    //移动banner添加
    public function banner_copy_add(){
        if ($this->request->isAjax()) {
            $data = $this->request->post();
            //上传图片
            $cate_img = (new \app\service\controller\File())->upload_single_img('head_img', 'head_img');
            if (is_array($cate_img)) {
                if ($cate_img['code'] == 1) {
                    $data['head_img'] = $cate_img['img_path'];
                } else {
                    $this->error($cate_img['msg']);
                }
            } else {
                unset($data['head_img']);
            }
            //更新栏目
            $res=Db::table('tp_banner_copy')->insert($data);
            $this->success('新增成功');
            exit;
        }
        $list=Db::table('tp_navigation')->select();
        $this->assign([
            'topbar' => ['首页设置', '移动banner设置','添加'],
            'list'=>$list
        ]);
        return $this->fetch();
    }
    //移动banner修改
    public function banner_copy_edit($id){
        if ($this->request->isAjax()) {
            $data = $this->request->post();
            //上传图片
            $cate_img = (new \app\service\controller\File())->upload_single_img('head_img', 'head_img');
            if (is_array($cate_img)) {
                if ($cate_img['code'] == 1) {
                    $data['head_img'] = $cate_img['img_path'];
                } else {
                    $this->error($cate_img['msg']);
                }
            } else {
                unset($data['head_img']);
            }
            //更新栏目
            $res=Db::table('tp_banner_copy')->where('id',$id)->update($data);
            $this->success('修改成功');
            exit;
        }
        $list=Db::table('tp_navigation')->select();
        $res=Db::table('tp_banner_copy')->where('id',$id)->find();
        $this->assign([
            'topbar' => ['首页设置', '移动banner设置','修改'],
            'seting' => Config::pull('cms_config'),
            'list'   =>$res,
            'lists' =>$list
        ]);
        return $this->fetch();
    }

    // 加盟管理
    public function join_list(){
        $lists=Db::table('tp_public')->paginate(15);
     
        $this->assign('active', 'home');
        $this->assign([
            'topbar' => ['首页设置', '加盟设置'],
            'seting' => Config::pull('cms_config'),
            'lists'  =>$lists
        ]);
        return $this->fetch();
    }
    //加盟管理修改
    public function join_edit($id){
        if ($this->request->isAjax()) {
            $data = $this->request->post();
            //更新栏目
            $res=Db::table('tp_public')->where('id',$id)->update($data);
            $this->success('修改成功');
            exit;
        }
        $res=Db::table('tp_public')->where('id',$id)->find();

        $this->assign([
            'topbar' => ['首页设置', '加盟设置','修改'],
            'seting' => Config::pull('cms_config'),
            'list'   =>$res
        ]);
        return $this->fetch();
    }
    //导航加载
    public function navigation_list(){
        $lists=Db::table('tp_navigation')->paginate(15);
        $this->assign('active', 'home');
        $this->assign([
            'topbar' => ['首页设置', '导航设置'],
            'seting' => Config::pull('cms_config'),
            'lists'  =>$lists
        ]);
        return $this->fetch();
    }
    //导航修改
    public function navigation_edit($id){
        if ($this->request->isAjax()) {
            $data = $this->request->post();
            //更新栏目
            $res=Db::table('tp_navigation')->where('id',$id)->update($data);
            $this->success('修改成功');
            exit;
        }
        $res=Db::table('tp_navigation')->where('id',$id)->find();

        $this->assign([
            'topbar' => ['首页设置', '导航设置','修改'],
            'seting' => Config::pull('cms_config'),
            'list'   =>$res
        ]);
        return $this->fetch();
    }
    public function brand_list(){
        $lists=Db::table('tp_brand')->paginate(15);
        $this->assign('active', 'home');
        $this->assign([
            'topbar' => ['首页设置', '品牌简介设置'],
            'seting' => Config::pull('cms_config'),
            'lists'  =>$lists
        ]);
        return $this->fetch();
    }
    //品牌简介添加
    public function brand_add(){
        if ($this->request->isAjax()) {
            $data = $this->request->post();
            //上传图片
            $cate_img = (new \app\service\controller\File())->upload_single_img('head_img', 'head_img');
            if (is_array($cate_img)) {
                if ($cate_img['code'] == 1) {
                    $data['head_img'] = $cate_img['img_path'];
                } else {
                    $this->error($cate_img['msg']);
                }
            } else {
                unset($data['head_img']);
            }
            //更新栏目
            $res=Db::table('tp_brand')->insert($data);
            $this->success('新增成功');
            exit;
        }
        $list=Db::table('tp_brand')->select();
        $this->assign([
            'topbar' => ['首页设置', '品牌简介设置','添加'],
            'list'=>$list
        ]);
        return $this->fetch();
    }
    //品牌简介修改
    public function brand_edit($id){
        if ($this->request->isAjax()) {
            $data = $this->request->post();
            //上传图片
            $cate_img = (new \app\service\controller\File())->upload_single_img('head_img', 'head_img');
            if (is_array($cate_img)) {
                if ($cate_img['code'] == 1) {
                    $data['head_img'] = $cate_img['img_path'];
                } else {
                    $this->error($cate_img['msg']);
                }
            } else {
                unset($data['head_img']);
            }
            //更新栏目
            $res=Db::table('tp_brand')->where('id',$id)->update($data);
            $this->success('修改成功');
            exit;
        }
        $res=Db::table('tp_brand')->where('id',$id)->find();
        $this->assign([
            'topbar' => ['首页设置', '品牌简介设置','修改'],
            'seting' => Config::pull('cms_config'),
            'list'   =>$res
        ]);
        return $this->fetch();
    }
    //多种卤菜系列 吃出健康 天天排队 款款热卖加载
    public function rm_list(){
        $lists=Db::table('tp_sale')->order('sort')->paginate(15);
        $this->assign('active', 'home');
        $this->assign([
            'topbar' => ['首页设置', '热卖展示图片设置'],
            'seting' => Config::pull('cms_config'),
            'lists'  =>$lists
        ]);
        return $this->fetch();
    }
    //多种卤菜系列 吃出健康 天天排队 款款热卖添加
    public function rm_add(){
        if ($this->request->isAjax()) {
            $data = $this->request->post();
            //上传图片
            $cate_img = (new \app\service\controller\File())->upload_single_img('head_img', 'head_img');
            if (is_array($cate_img)) {
                if ($cate_img['code'] == 1) {
                    $data['head_img'] = $cate_img['img_path'];
                } else {
                    $this->error($cate_img['msg']);
                }
            } else {
                unset($data['head_img']);
            }
            //更新栏目
            $res=Db::table('tp_sale')->insert($data);
            $this->success('新增成功');
            exit;
        }
        $list=Db::table('tp_sale')->select();
        $this->assign([
            'topbar' => ['首页设置', '热卖展示图片设置','添加'],
            'list'=>$list
        ]);
        return $this->fetch();
    }
    //多种卤菜系列 吃出健康 天天排队 款款热卖修改
    public function rm_edit($id){
        if ($this->request->isAjax()) {
            $data = $this->request->post();
            //上传图片
            $cate_img = (new \app\service\controller\File())->upload_single_img('head_img', 'head_img');
            if (is_array($cate_img)) {
                if ($cate_img['code'] == 1) {
                    $data['head_img'] = $cate_img['img_path'];
                } else {
                    $this->error($cate_img['msg']);
                }
            } else {
                unset($data['head_img']);
            }
            //更新栏目
            $res=Db::table('tp_sale')->where('id',$id)->update($data);
            $this->success('修改成功');
            exit;
        }
        $res=Db::table('tp_sale')->where('id',$id)->find();
        $this->assign([
            'topbar' => ['首页设置', '热卖展示图片设置','修改'],
            'seting' => Config::pull('cms_config'),
            'list'   =>$res
        ]);
        return $this->fetch();
    }

    // 人气菜品设置
    public function text_list(){

        $lists=Db::table('tp_series_par')
            ->leftJoin('tp_series','tp_series.id=tp_series_par.series_id')
            ->field('tp_series.title,tp_series_par.*')
            ->where('sale',9)
            ->paginate(10);
        $this->assign('active','home');
        $this->assign([
            'topbar' => ['人气菜品设置', '麻辣鲜香详情设置'],
            'seting' => Config::pull('cms_config'),
            'lists'  =>$lists
        ]);
        return $this->fetch();
    }
    //人气菜品详情添加
    public function text_add(){
        if ($this->request->isAjax()) {
            $data = $this->request->post();
            //上传图片
            $cate_img = (new \app\service\controller\File())->upload_single_img('head_img', 'head_img');
            if (is_array($cate_img)) {
                if ($cate_img['code'] == 1) {
                    $data['head_img'] = $cate_img['img_path'];
                } else {
                    $this->error($cate_img['msg']);
                }
            } else {
                unset($data['head_img']);
            }
            //更新栏目
            //9为首页热卖
            $data['sale']=9;
            $res=Db::table('tp_series_par')->insert($data);
            $this->success('新增成功');
            exit;
        }
        $list=Db::table('tp_series')->select();
        $this->assign([
            'topbar' => ['人气菜品设置', '麻辣鲜香详情设置','添加'],
            'list'=>$list
        ]);
        return $this->fetch();
    }
    //人气菜品详情修改
    public function text_edit($id){
        if ($this->request->isAjax()) {
            $data = $this->request->post();
            //上传图片
            $cate_img = (new \app\service\controller\File())->upload_single_img('head_img', 'head_img');
            if (is_array($cate_img)) {
                if ($cate_img['code'] == 1) {
                    $data['head_img'] = $cate_img['img_path'];
                } else {
                    $this->error($cate_img['msg']);
                }
            } else {
                unset($data['head_img']);
            }
            //更新栏目

            $res=Db::table('tp_series_par')->where('id',$id)->update($data);
            $this->success('修改成功');
            exit;
        }
        //$res=Db::table('tp_series_par')->where('id',$id)->find();
        $res=Db::table('tp_series_par')
            ->leftJoin('tp_series','tp_series.id=tp_series_par.series_id')
            ->field('tp_series.title,tp_series.id as pid,tp_series_par.*')
            ->where('tp_series_par.id',$id)->find();
        $series=Db::table('tp_series')->select();
        $this->assign([
            'topbar' => ['人气菜品设置', '热门菜品设置','修改'],
            'seting' => Config::pull('cms_config'),
            'list'   =>$res,
            'series' =>$series
        ]);
        return $this->fetch();
    }

}
